$(document).ready(function() {
	
	$("#restart").hide();
	
	$("#start").click(function() {
		$.get("/pokdeng", { type : "start" }).done(function(data) {
			console.log (data);
			Display(cleanData(data));
			$("#start").hide();
		});
	});
	
	$("#restart").click(function() {
		$.get("/pokdeng", { type : "restart" }).done(function(data) {
			console.log (data);
			Display(cleanData(data));
		});
	});
	
	$("#draw").click(function() {
		$.get("/pokdeng", { type : "draw" }).done(function(data) {
			console.log(data);
			Display(cleanData(data));
		});
	});
	
	$("#hold").click(function() {
		$.get("/pokdeng", { type : "hold" }).done(function(data) {
			console.log(data);
			Display(cleanData(data));
		});
	});
	
	function Display(arr) {
		$("#result").val("Result: "+arr[0][1]);
		$("#balance").val("Balance: "+arr[0][5]);
		$("#player_hand").html(arr[1][2]+"\n"+arr[1][3]+"\n"+arr[1][4]+"\n");
		$("#banker_hand").html(arr[2][2]+"\n"+arr[2][3]+"\n"+arr[2][4]+"\n");
		
		if(arr[0][2] != 0) {
			$("#draw").attr("disabled","disabled");
			$("#hold").attr("disabled","disabled");
			$("#restart").show();
		} else {
			$("#draw").removeAttr("disabled","disabled");
			$("#hold").removeAttr("disabled","disabled");
			$("#restart").hide();
		}
	}
});

function cleanData(source) {
	array = source.split(",");
	var data = Array();
	for (var i = 0; i < array.length; i++) {
		data[i] = array[i].split("/");
	}
	return data;
}

